package com.payment.web.controller.app.shopping;

import com.payment.common.base.AjaxResult;
import com.payment.framework.web.base.BaseController;
import com.payment.system.domain.ShoppingAddress;
import com.payment.system.service.IShoppingAddressService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * 收获地址 信息操作处理
 *
 * @author payment
 * @date 2019-01-18
 */
@Controller
@RequestMapping("/app/shoppingAddress")
public class ShoppingAddressController extends BaseController {
    private static final Logger log = LoggerFactory.getLogger(ShoppingAddressController.class);


    @Autowired
    private IShoppingAddressService shoppingAddressService;

    /**
     * 查询收获地址列表
     */
    @RequestMapping("/list")
    @ResponseBody
    public AjaxResult list(String merchantId) {
        return shoppingAddressService.selectShoppingAddressListBymerchantId(merchantId);
    }

    /**
     * 新增保存收获地址
     */
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ShoppingAddress shoppingAddress) {
        return shoppingAddressService.insertShoppingAddress(shoppingAddress);
    }

    /**
     * 查询商户地址详情
     *
     * @param merchantId
     * @param addressId
     * @return
     */
    @RequestMapping("/selectAddressById")
    @ResponseBody
    public AjaxResult selectAddressById(String merchantId, String addressId) {
        return shoppingAddressService.selectAddressById(merchantId, addressId);
    }

    /**
     * 修改收获地址
     */
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ShoppingAddress shoppingAddress) {
        return shoppingAddressService.updateShoppingAddress(shoppingAddress);
    }

    /**
     * 删除收获地址
     */
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        if (ids.length() < 0) {
            return error(1, "删除收获地址失败：缺失地址id！");
        }
        try {
            shoppingAddressService.deleteShoppingAddressByIds(ids);
            return success("删除收获地址成功！");
        } catch (Exception e) {
            log.error("删除收获地址失败！请联系客服查询原因", e);
            return AjaxResult.error("删除收获地址失败！请联系客服查询原因");
        }
    }

    /**
     * 设置未默认地址
     *
     * @param merchantId
     * @param addressId
     * @return
     */
    @PostMapping("/updatweDefaultAddress")
    @ResponseBody
    public AjaxResult updatweDefaultAddress(String merchantId, String addressId) {
        try {
            return shoppingAddressService.updatweDefaultAddress(merchantId, addressId);
        } catch (Exception e) {
            log.error("设置未默认地址失败，设置未默认地址="+e.toString());
            return AjaxResult.error("设置未默认地址失败，请联系客服查询原因");
        }
    }
}
